//    Copyright 2021 Cardinal
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

import java.awt.BorderLayout;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

public class homework_2 {
    public static void main(String[] args) {
        var window = new JFrame("N");
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.setLocationRelativeTo(null);
        window.setSize(640, 480);
        window.setLayout(new BorderLayout());
        var panel = new JPanel();

        var ta_txtShower = new JTextArea();
        var valueHashMap = new HashMap<String, Boolean>();
        var customListener = new NActionListener(valueHashMap, ta_txtShower);

        window.add(panel, BorderLayout.NORTH);

        panel.add(new JLabel("兴趣"));
        var cb_badminton = new JCheckBox("羽毛球");
        var cb_tableTennis = new JCheckBox("乒乓球");
        var cb_sing = new JCheckBox("唱歌");
        cb_badminton.addChangeListener(customListener);
        cb_tableTennis.addChangeListener(customListener);
        cb_sing.addChangeListener(customListener);
        panel.add(cb_badminton);
        panel.add(cb_tableTennis);
        panel.add(cb_sing);

        panel.add(new JLabel("性别"));
        var gb_sex = new ButtonGroup();
        var rb_male = new JRadioButton("男");
        var rb_female = new JRadioButton("女");
        rb_male.addChangeListener(customListener);
        rb_female.addChangeListener(customListener);
        gb_sex.add(rb_male);
        gb_sex.add(rb_female);
        panel.add(rb_male);
        panel.add(rb_female);

        window.add(new JScrollPane(ta_txtShower), BorderLayout.CENTER);

        window.setVisible(true);
    }
}

class NActionListener implements ChangeListener {
    public NActionListener(HashMap<String, Boolean> vHashMap, JTextArea targetArea) {
        targetTextArea = targetArea;
        valueHashMap = vHashMap;
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        {
            String txt = null;
            txt = doMethod(e.getSource(), "getText", txt);
            Boolean needShow = false;
            needShow = doMethod(e.getSource(), "isSelected", needShow);
            if (needShow != null) {
                valueHashMap.put(txt, needShow);
            }
        }
        String txt = "";
        for (var entry : valueHashMap.entrySet()) {
            if (entry.getValue()) {
                txt += entry.getKey();
                txt += " ";
            }
        }
        targetTextArea.setText(txt);
    }

    private <ReturnType> ReturnType doMethod(Object o, String methodName, ReturnType returnType, Object... args) {
        var methods = o.getClass().getMethods();
        for (var m : methods) {
            if (m.getName().equals(methodName)) {
                try {
                    return (ReturnType) m.invoke(o, args);
                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                    ex.printStackTrace();
                    return returnType;
                }
            }
        }
        return returnType;
    }

    private HashMap<String, Boolean> valueHashMap;
    private JTextArea targetTextArea;
}